package com.ccsu.mymarket.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccsu.mymarket.dao.BookDao;
import com.ccsu.mymarket.dao.ShopDao;
import com.ccsu.mymarket.entity.Book;
import com.ccsu.mymarket.entity.Shop;
import com.ccsu.mymarket.entity.User;
import com.ccsu.mymarket.service.ShopService;
import com.ccsu.mymarket.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopServiceImpl extends ServiceImpl<ShopDao,Shop> implements ShopService {
    private ShopDao shopDao;
    private UserService userService;
    private BookDao bookDao;

    public ShopServiceImpl(@Autowired ShopDao shopDao, @Autowired UserService userService,@Autowired BookDao bookDao) {
        this.shopDao = shopDao;
        this.userService = userService;
        this.bookDao = bookDao;
    }

    @Override
    public boolean deleteShop(Integer id) {
//        删除店铺  ->查找用户id -> 取消用户商家身份，改为买家 ->删除店铺下的所有商品
        if (this.removeById(id)){
            Shop shop=this.getOne(new LambdaQueryWrapper<Shop>()
                    .eq(Shop::getId,id).select(Shop::getAcc));
            if (shop==null){
                return false;
            }
            Integer userId=shop.getAcc();
            userService.update(new LambdaUpdateWrapper<User>()
                    .set(User::getRole,1).eq(User::getId,userId));

            bookDao.delete(new LambdaQueryWrapper<Book>().eq(Book::getShop,id));

            return true;
        }
        return false;
    }

    @Override
    public List<Shop> getShopForAdmin(Integer pagenum, Integer pagesize) {
        return shopDao.getShopForAdmin(pagenum,pagesize);
    }
}
